import type { PlatformTypeValue } from '../../../interfaces'
import { GreasyForkPageType } from '../../../../lib/page_handler/platform/greasyfork/types'
import { SUPPORTED_PLATFORMS } from '../../platform'
import { TITLE_STYLES } from './index'

type TitleSelectorMapping = {
  [key in PlatformTypeValue]: {
    secondary: Partial<Record<GreasyForkPageType, string>>
    tertiary: Partial<Record<GreasyForkPageType, string>>
  }
}

/**
 * 标题选择器映射
 */
export const TITLE_SELECTOR_MAPPING: TitleSelectorMapping = {
  [SUPPORTED_PLATFORMS.GREASY_FORK]: {
    secondary: {
      [GreasyForkPageType.INDEX]: '.width-constraint [id^="home-step"] h3',
      [GreasyForkPageType.USER_PROFILE]: '.width-constraint h3',
      [GreasyForkPageType.SEARCH]: '.width-constraint h3',
      [GreasyForkPageType.HELP]: '.width-constraint h3',
      [GreasyForkPageType.CODE_RULES]: '.width-constraint h4',
      [GreasyForkPageType.EXTERNAL_SCRIPTS]: '.width-constraint h3',
      [GreasyForkPageType.INSTALLING_USER_SCRIPTS]: '.width-constraint [id^="home-step"] h3',
      [GreasyForkPageType.PRIVACY]: '.width-constraint h2',
      [GreasyForkPageType.EDIT_SIGN_IN]: '.width-constraint h3',
      [GreasyForkPageType.NOTIFICATION_SETTINGS]: '.width-constraint .form-control h3',
      [GreasyForkPageType.SETS_NEW]: '.width-constraint h3',
      [GreasyForkPageType.WEBHOOK_INFO]: '.width-constraint h3',
      [GreasyForkPageType.SCRIPT_DERIVATIVES]: '.width-constraint #script-content h3',
      [GreasyForkPageType.SCRIPT_ADMIN]: '.width-constraint #script-content h3',
    },
    tertiary: {
      [GreasyForkPageType.EXTERNAL_SCRIPTS]: '.width-constraint h4',
    },
  },
}

/**
 * 获取标题选择器
 * @returns 所有标题选择器的逗号分隔字符串
 */
export function getAllTitleSelector(): string {
  return [
    TITLE_STYLES.SECONDARY.className,
    TITLE_STYLES.TERTIARY.className,
  ].map(item => `.${item}`).join(',')
}
